package com.example.zg3_park.CarType

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.lib_base.entity.CarTypeEntityItem
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ItemParkBinding


class CarTypeAdapter:PagingDataAdapter<CarTypeEntityItem, CarTypeAdapter.CarTypeViewHolder>(diff) {

    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null

    companion object{
        val diff = object :ItemCallback<CarTypeEntityItem>() {
            override fun areItemsTheSame(
                oldItem: CarTypeEntityItem,
                newItem: CarTypeEntityItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: CarTypeEntityItem,
                newItem: CarTypeEntityItem
            ): Boolean {
                return oldItem.id == newItem.id
            }
        }
    }


    class CarTypeViewHolder(view:View):ViewHolder(view) {
        val binding = DataBindingUtil.bind<ItemParkBinding>(view)
    }

    override fun onBindViewHolder(holder: CarTypeViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.car = item

        holder.binding?.tvStatus?.apply {
            when(item?.spaceStatus) {
                "空闲" -> {
                    setBackgroundResource(R.drawable.color_kongxian)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.kongxian))
                }
                "已占用" -> {
                    setBackgroundResource(R.drawable.color_zhanyong)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.yizhanyong))
                }
                else -> {
                    setBackgroundResource(R.drawable.color_weihu)
                    setTextColor(holder.itemView.context.resources.getColor(R.color.weihu))
                }
            }
        }

        //点击事件
        holder.binding?.car = item
        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position,item!!)
            return@setOnClickListener
        }
        holder.itemView.setOnLongClickListener {
            this.mOnItemLongClickListener?.onItemLongClick(item!!)
            return@setOnLongClickListener true
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CarTypeViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return CarTypeViewHolder(view)
    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
}

interface OnItemClickListener {
    fun onItemClick(position: Int,item:CarTypeEntityItem)
}

interface OnItemLongClickListener {
    fun onItemLongClick(item:CarTypeEntityItem): Boolean
}

